package com.meitianhui.convenitenbuy.module.home.ui.activity;

import android.app.Dialog;
import android.content.Intent;
import android.net.Uri;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.meitianhui.convenitenbuy.App;
import com.meitianhui.convenitenbuy.R;
import com.meitianhui.convenitenbuy.module.home.contract.HomeScanningContract;
import com.meitianhui.convenitenbuy.module.home.di.DaggerHomeComponent;
import com.meitianhui.convenitenbuy.module.home.di.HomeModule;
import com.meitianhui.convenitenbuy.module.home.model.bean.MemberInfo;
import com.meitianhui.convenitenbuy.module.home.model.bean.OfficialStore;
import com.meitianhui.convenitenbuy.module.home.presenter.HomeScanningPresenter;
import com.meitianhui.convenitenbuy.util.GlideUtils;
import com.meitianhui.convenitenbuy.util.LogUtils;
import com.meitianhui.convenitenbuy.util.RxTimerUtils;
import com.meitianhui.convenitenbuy.util.RxUtils;
import com.meitianhui.convenitenbuy.util.StringUtils;
import com.meitianhui.convenitenbuy.util.TipUtils;
import com.meitianhui.convenitenbuy.widget.CircleImageView;
import com.meitianhui.convenitenbuy.zxing.activity.CaptureActivity;

import javax.inject.Inject;

import rx.functions.Action1;

import static com.meitianhui.convenitenbuy.R.id.tv_home_scanning_dialog_quit;

/**
 * Created by Administrator on 2017/11/8.
 */

public class HomeScanCodeActivity extends CaptureActivity implements HomeScanningContract.View {

    private TextView mTvStatus;
    private RxTimerUtils.IRxNext failNext;
    private RxTimerUtils mRx;
    @Inject
    HomeScanningPresenter presenter;
    private Dialog mDialog;
    private CircleImageView portrait;

    @Override
    protected void initId() {
        super.initId();
        mTvStatus = getViewById(R.id.tv_home_scanning_status);
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        DaggerHomeComponent.builder()
                .appComponent(App.getInstance().getAppComponent())
                .homeModule(new HomeModule(this))
                .build()
                .inject(this);
        mRx = new RxTimerUtils();
        failNext = new RxTimerUtils.IRxNext() {
            @Override
            public void doNext(long number) {
                mTvStatus.setText("");
                setReCapture();
            }
        };
    }

    @Override
    protected int setView() {
        return R.layout.home_scanning_container;
    }

    @Override
    protected void receiveResult(String result) {
        if (result == null) {
            mTvStatus.setText(R.string.wallet_scanning_fail);
            mRx.timer(8000, failNext);
        }
        mTvStatus.setText(R.string.wallet_scanning_waiting);
        if (StringUtils.isPhone(result)) {
            presenter.getMemberInfo(result);
        } else if (result.startsWith("http") || result.startsWith("https")) {
            showInfoDialog(result);
        } else {
            presenter.getStoreInfo(result);
        }


    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mRx.cancel();
    }

    @Override
    public void showError(String msg) {

    }

    @Override
    public void getMemberInfoBack(MemberInfo bean, String msg) {
        mTvStatus.setText("");
        if (bean == null) {
            TipUtils.showToast(App.getInstance(), msg);
            mRx.timer(8000, failNext);
            return;
        }
        showInfoDialog(bean);
    }

    @Override
    public void getPortraitBack(String img) {
        GlideUtils.setRectImage(this,img, R.mipmap.icon_default_picture_store, R.mipmap.icon_default_picture_store, portrait);
    }

    @Override
    public void getStoreInfoBack(OfficialStore bean, String msg) {
        mTvStatus.setText("");
        if (bean == null) {
            TipUtils.showToast(App.getInstance(), getString(R.string.home_scanning_dialog_error));
            mRx.timer(8000, failNext);
            return;
        }
        showInfoDialog(bean);
    }


    @Override
    public void collectStoreBack(String msg) {
        mDialog.dismiss();
        if (msg == null)
            TipUtils.showToast(App.getInstance(), getString(R.string.home_scanning_collect), 1);
        else
            TipUtils.showToast(App.getInstance(), getString(R.string.home_scanning_collect_fail));
        mRx.timer(8000, failNext);
    }

    private void setImagePortrait(String path) {
        LogUtils.e("==========================="+path);
        if (TextUtils.isEmpty(path)) return;
        GlideUtils.setRectImage(this,path, R.mipmap.icon_default_picture_store, R.mipmap.icon_default_picture_store, portrait);

//        presenter.getMemberPortrait(path);

    }

    private void setImageLevel(ImageView imageLevel, String level) {
        if (level.equals("1")) {
            imageLevel.setImageResource(R.mipmap.consumer_grade_1);
        } else if (level.equals("2")) {
            imageLevel.setImageResource(R.mipmap.consumer_grade_2);
        } else if (level.equals("3")) {
            imageLevel.setImageResource(R.mipmap.consumer_grade_3);
        } else if (level.equals("4")) {
            imageLevel.setImageResource(R.mipmap.consumer_grade_4);
        } else if (level.equals("5")) {
            imageLevel.setImageResource(R.mipmap.consumer_grade_5);
        } else if (level.equals("6")) {
            imageLevel.setImageResource(R.mipmap.consumer_grade_6);
        }
    }

    private void showInfoDialog(Object info) {
        mDialog = new Dialog(this, R.style.dialog_loading);
        mDialog.setContentView(R.layout.dialog_greeting_consumer);
//            mDialog.setCancelable(false);
//            mDialog.setCanceledOnTouchOutside(false);
        TextView name = (TextView) mDialog.findViewById(R.id.tv_home_scanning_dialog_name);
        ImageView grade = (ImageView) mDialog.findViewById(R.id.iv_home_scanning_dialog_grade);
        TextView phone = (TextView) mDialog.findViewById(R.id.tv_home_scanning_dialog_mobile);
        portrait = (CircleImageView) mDialog.findViewById(R.id.iv_home_scanning_dialog_img);
        Button greet = (Button) mDialog.findViewById(R.id.btn_home_scanning_dialog);
        ImageView quit = (ImageView) mDialog.findViewById(tv_home_scanning_dialog_quit);
        RxUtils.bindEvents(quit, this);
        if (info instanceof MemberInfo) {
            MemberInfo bean = (MemberInfo) info;
            greet.setText(getString(R.string.home_scanning_dialog_greeting));
            name.setText(bean.getNick_name());
            phone.setText(bean.getMobile());
            setImageLevel(grade, bean.getGrade());
            setImagePortrait(bean.getHead_pic_path());
            RxUtils.bindEvents(greet, new Action1<View>() {
                @Override
                public void call(View view) {
                    mDialog.dismiss();
                    TipUtils.showToast(HomeScanCodeActivity.this, getString(R.string.home_scanning_dialog_greet_tip), 1);
                    mRx.timer(8000, failNext);
                }
            });
        } else if (info instanceof OfficialStore) {
            OfficialStore bean = (OfficialStore) info;
            greet.setText(getString(R.string.home_scanning_store_collect));
            name.setText(bean.getStores_name());
            phone.setText(bean.getAddress());
            setImagePortrait(bean.getNeighbor_pic_path());
            RxUtils.bindEvents(greet, new Action1<View>() {
                @Override
                public void call(View view) {
                    mDialog.dismiss();
                    presenter.collectStore(bean);
                }
            });
        } else {
            portrait.setImageResource(R.mipmap.home_scan_url);
            String result = (String) info;
            greet.setText(getString(R.string.home_scanning_jump_url));
            phone.setText(result);
            RxUtils.bindEvents(greet, new Action1<View>() {
                @Override
                public void call(View view) {
                    try {
                        Intent intent = new Intent();
                        intent.setAction("android.intent.action.VIEW");
                        Uri content_url = Uri.parse(result);
                        intent.setData(content_url);
                        startActivity(intent);
                    } catch (Exception e) {
                        TipUtils.showToast(HomeScanCodeActivity.this, getString(R.string.home_scanning_dialog_error));
                        mRx.timer(8000, failNext);
                    }
                    finish();
                }
            });
        }

        mDialog.show();
    }

    @Override
    public void call(View v) {
        super.call(v);
        switch (v.getId()){
            case R.id.tv_home_scanning_dialog_quit:
                mDialog.dismiss();
        }
    }
}
